package risk.export.record.api;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import risk.export.record.mapper.pojo.TransactionRecordBean;
import risk.export.record.mapper.record.RecordMapper;
import risk.export.record.utils.TimeUtils;
import risk.export.record.utils.response.ResponseUtils;

import java.util.HashMap;
import java.util.List;

import static risk.export.record.utils.DateTimeValidator.isValidDateTime;
import static risk.export.record.utils.DateTimeValidator.isValidTimeRange;

@RestController
@RequestMapping(value = "/record", produces = {"application/json;charset=UTF-8"})
public class Export_000_000_001 {
    private static final Logger logger = LoggerFactory.getLogger(Export_000_000_001.class);


    @Qualifier("MainSqlSessionFactory")
    private final SqlSessionFactory sqlSessionFactory;

    @Qualifier("JedisPool")
    private final JedisPool jedisPool;

    public Export_000_000_001(JedisPool jedisPool, SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
        this.jedisPool = jedisPool;
    }


    //查询接口
    @PostMapping(value = "/000_000_000")
    public String doPost(HttpServletRequest req) {
        logger.error("-----请求开始---->{}", TimeUtils.getCurrentTime());
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            String requestBody = req.getReader().readLine();
            JSONObject jsonObject = (JSONObject) JSON.parse(requestBody);
            String key = jsonObject.getString("key");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            logger.error("startTime---->{}", startTime);
            logger.error("endTime---->{}", endTime);
            //校验参数
            if (key == null || key.isEmpty()) {
                return ResponseUtils.generateFail("key为空");
            }
            if (!key.equals("5ab29120ffa65d289bf31f7db39ed0f9")) {
                return ResponseUtils.generateFail("key错误");
            }
            if (!isValidDateTime(startTime)) {
                return ResponseUtils.generateFail("请输入合法的开始时间");
            }
            if (!isValidDateTime(endTime)) {
                return ResponseUtils.generateFail("请输入合法的结束时间");
            }
            if (!isValidTimeRange(startTime, endTime)) {
                return ResponseUtils.generateFail("开始时间不晚于结束时间");
            }
            //开始查库
            RecordMapper mapper = session.getMapper(RecordMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("startTime", startTime);
            params.put("endTime", endTime);
            List<TransactionRecordBean> transactionRecordBeans = mapper.queryFromTime(params);
            logger.error("-----请求结束--查库-->{}", TimeUtils.getCurrentTime());
            logger.error("=================================================");
            return ResponseUtils.generateSucceed(transactionRecordBeans);
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}
